window.onload = initialize;
var searchnode;
var request;

function initialize() {
    // Get our SELECTS and setup an event handler
	button= document.getElementById("categorymod");
	button.onchange= searchLookup;
	button= document.getElementById("typemod");
	button.onchange= searchLookup;
	button= document.getElementById("finishmod");
	button.onchange= searchLookup;
	button= document.getElementById("materialmod");
	button.onchange= searchLookup;
	button= document.getElementById("shapemod");
	button.onchange= searchLookup;

	button= document.getElementById("search_query2");
	button.onkeyup= searchLookup;


	
	
	
	
	
	/*searchnode = document.getElementById("search-text");
	request = null;
	searchnode.onkeyup = getCompletedValue; */
	createRequest();
	//searchLookup();	
}

/* 
 * 
 * This is the function that is called when the "vote" button is clicked
 * It gets the vote from the radio input and sends it to the appropriate URL for processing.
 * It then sets up the callback function to process the results.
 * And it provides some information about what's going on.
 *
 */
function searchLookup() {



	//initialize the variable with an average rating
	var category=0;
	var parent= document.getElementById("categorymod");
	var options= parent.getElementsByTagName("option");
	for (i=0; i<options.length; i++) {
		var current= options[i];
		if (current.selected) {
			category = current.value;
		}
	}
	
	var type=0;
	var parent= document.getElementById("typemod");
	var options= parent.getElementsByTagName("option");
	for (i=0; i<options.length; i++) {
		var current= options[i];
		if (current.selected) {
			type = current.value;
		}
	}
	
	var finish=0;
	var parent= document.getElementById("finishmod");
	var options= parent.getElementsByTagName("option");
	for (i=0; i<options.length; i++) {
		var current= options[i];
		if (current.selected) {
			finish = current.value;
		}
	}
	
	var material=0;
	var parent= document.getElementById("materialmod");
	var options= parent.getElementsByTagName("option");
	for (i=0; i<options.length; i++) {
		var current= options[i];
		if (current.selected) {
			material = current.value;
		}
	}
	
	var shape=0;
	var parent= document.getElementById("shapemod");
	var options= parent.getElementsByTagName("option");
	for (i=0; i<options.length; i++) {
		var current= options[i];
		if (current.selected) {
			shape = current.value;
		}
	}
	
	var words=null;
	var field= document.getElementById("search_query2");
	words= field.value;
	
	//* Here is the url we will need to call, with the username omitted 
	//var url="http://localhost/diningfurnitureshowroom/search-ajax.php?categorymod=" + category;
	
	var link= document.location.href;
		link= link.replace("search.php", "search-ajax.php");
	var qMark= link.indexOf('?');
		link= link.substring(0,qMark);
	
	var url= link +"?search_query2="+ words +"&categorymod=" + category + "&typemod=" + type + "&finishmod=" + finish + "&materialmod=" + material + "&shapemod=" + shape + "&queryModified=1&searchmodify=Go"
	
	//alert(url);

	//* Make the Ajax call 
	request.open("GET", url, true);
	request.onreadystatechange = processSearch;
	request.send(null);


}

/*
 *
 * Actually process the results of the AJAX call
 *
 */
function processSearch() {

	//textarea.nodeValue="Calculating...";
	var output= document.getElementById("output");
	output.innerHTML="Loading...";
	
	if (request.readyState < 4)
	{return;}
	
	// If there's not an answer, quit
	if (request.responseText == "") { 
		output.innerHTML="Your search returned no results. Please broaden your criteria. ";
		return;
	}

	output.innerHTML=request.responseText;	
}


/*function getCompletedValue() {
	if (this.keyCode == 8) return;
	var url="searchanswer.php?searchtext="+escape(searchnode.value);
	request.open("GET",url,true);
	request.onreadystatechange = fillCompletedValue;
	request.send(null);
}

function fillCompletedValue() {
	if (request.readyState < 4) {return;}
	
	response = request.responseText;
	if (trim(response)=="") {return;}
	
	responsearray = response.split("/");
	userentry = searchnode.value;
	
	// Check that the returned string is the initial string of
	// the user's input thus far
	if (userentry == responsearray[0].substr(0,userentry.length)) {
		searchnode.value = responsearray[0];
	}
}

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '')
};*/


function createRequest() {
	// From "Head Rush Ajax" by Brett McLaughlin
	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = null;
			}
		}
	}
	
	if (request == null) {
		alert("Error creating request object!");
	}

}